<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class ShopEco extends ShopFrontEndController
{
    public $shopeco_model = null;

	public function __construct($params) {
		parent::__construct($params);
		$this->load->library('mypaging');
	}

	public function renderView($viewName, $data = null) {
	   if($this->shopeco_model==null){
	       $this->shopeco_model = new Shopeco_Model();
	   }

		$theme_path          = 'shop-themes/'. $this->shop->template;
		$data['shop']        = $this->shop;
		$data['shop_page']   = $this->shop_model->getShopPages($this->shop->show_page);
		$data['shop_cate']   = $this->shopeco_model->getAllShopCategory($this->shop->getSlug());
		$data['html']        = $this->load->view($theme_path . '/pages/' . $viewName . '.html.php', $data, true);
		$this->load->view($theme_path . '/layout/layout.html.php', $data);
	}

	public function index($params=array()) {
		$pageIndex = isset($params[1]) ? $params[1] : 1;

		$this->mypaging->setLimit($this->shop->shop_product_num);
		$this->mypaging->setSuffix($this->config->item('url_suffix'));
		$this->mypaging->setCount($this->shop_product_model->countAllProduct(array('a.shop_id'=>$this->shop->getId()), false));
		$this->mypaging->setMainURL(shop_product_paging_url($this->shop->getSlug()));

		$pagingBar = $this->mypaging->getBar($pageIndex);
		$offset    = ($pageIndex - 1) * $this->shop->shop_product_num;
		$select    = array(
			"a.title AS title, a.slug AS slug, a.sale_flg AS sale_flg",
			"b.id as product_detail_id, b.description, b.code, b.image, b.price_new, b.price_old,e.image_folder AS shop_image_folder"
			);
		$aProduct    = $this->shop_product_model->paging(PRODUCT_LIMIT, $offset, array('a.shop_id'=>$this->shop->getId()), $select, false);
		$shop_slides = $this->shop_model->getBannerSlide($this->shop->getSlug());
		$aReturn     = array(
			'shop_slides' => $shop_slides,
			'aProduct'    => $aProduct,
			'pagingBar'   => $pagingBar
			);
		$this->renderView('index', $aReturn);
	}

	public function getAllProduct($params=array()) {
		$this->shopeco_model = new Shopeco_Model();
		$pageIndex = isset($params[1]) ? $params[1] : 1;

		$this->mypaging->setLimit($this->shop->shop_product_num);
		$this->mypaging->setSuffix($this->config->item('url_suffix'));
		$this->mypaging->setCount($shopeco_model->countAllProduct(array('a.shop_id'=>$this->shop->getId()), false));
		$this->mypaging->setMainURL(shop_product_page_paging_url($this->shop->getSlug()));

		$pagingBar = $this->mypaging->getBar($pageIndex);
		$offset    = ($pageIndex - 1) * $this->shop->shop_product_num;
		$select    = array(
			"a.title AS title, a.slug AS slug, a.sale_flg AS sale_flg",
			"b.id as product_detail_id, b.description, b.code, b.image, b.price_new, b.price_old,e.image_folder AS shop_image_folder"
			);
		$aProduct    = $shopeco_model->paging(PRODUCT_LIMIT, $offset, array('a.shop_id'=>$this->shop->getId()), $select, false);
		$aReturn     = array(
			'pageTitle'   =>'Sản phẩm',
			'aProduct'    => $aProduct,
			'pagingBar'   => $pagingBar
			);
		$this->renderView('products', $aReturn);
	}

	public function contact($shop_slug) {
		$this->load->model('repo/user_model', 'user_model');
		$user_info = $this->user_model->getUserByShopSlug($this->shop->getSlug());
		$this->frontend_lib->setAJs("https://www.google.com/recaptcha/api.js?hl=vi");
		$aReturn = array('pageTitle'=>'Liên hệ', 'user_info'=>$user_info);
		$this->renderView('contact', $aReturn);
	}

	public function search($params=array() ) {
		$pageIndex= count($params)>0 && isset($params[1]) ? $params[1] : 1;
		$searchkey = $this->input->get('s', true);

		$this->mypaging->setLimit($this->shop->shop_product_num);
		$this->mypaging->setSuffix($this->config->item('url_suffix'));
		$this->mypaging->setCount($this->shop_product_model->countFrontSearchProduct($this->shop->getId(), $searchkey));
		$this->mypaging->setMainURL(shop_product_search_paging_url($this->shop->getSlug()));

		$pagingBar = $this->mypaging->getBar($pageIndex, array('s' => $searchkey));
		$offset    = ($pageIndex - 1) * $this->shop->shop_product_num;
		$aProduct  = $this->shop_product_model->searchFrontProduct($this->shop->getId(), $searchkey, $this->shop->shop_product_num, $offset);
		$aReturn   = array(
			'pageTitle'      => 'Sản phẩm',
			'aProduct'       => $aProduct,
			'pagingBar'      => $pagingBar,
			'searchkey'      => $searchkey
			);
		$this->renderView('search', $aReturn);
	}

	public function getCartContent(){
		echo $this->load->view('shop-themes/'. $this->shop->template . '/modules/cart.html.php', array(), true);
		die();
	}

	public function getProductModal($params=array()){
		$ref = $this->input->post('ref', true);
		$product_slug = $params[1];
		$select = array(
			'a.id, a.title, a.slug, a.shop_cate_id, a.active_flg, a.deleted_at,
			f.title AS shop_cate_title, f.slug AS shop_cate_slug,
			e.image_folder AS shop_image_folder'
			);
		$product = $this->shop_product_model->get($product_slug, array(), $select, false);
		if($product == null) { show_404(); }
		$this->load->model('repo/product_model', 'product_model');
		$productDetails = $this->product_model->getProductDetails($product->getId());
		$aReturn = array(
			'ref'            => $ref,
			'product'        => $product,
			'productDetails' => $productDetails
			);
		echo $this->load->view('shop-themes/'. $this->shop->template . '/modules/product.html.php', $aReturn, true);
		die();
	}
}
?>